package nc.tb.cube.cell;

import java.util.HashMap;
import java.util.Iterator;

import nc.tb.dim.DimDef;
import nc.tb.dim.DimMember;
import nc.tb.dim.data.DimManager;
import nc.tb.pub.profile.IProfileObject;

public class OlapViewCellFilter extends PlanCellFilter implements
		IProfileObject {

	public OlapViewCellFilter() {
		super();
	}

	public String toDbString() {
		StringBuffer sb = new StringBuffer();
		HashMap<DimDef, HashMap<DimMember, Object>> hm = getAcceptDimVector();
		if (hm != null) {
			for (Iterator<DimDef> it = hm.keySet().iterator(); it.hasNext(); ) {
				DimDef dd = it.next();
				sb.append(dd.getObjcode()).append("=");
				HashMap<DimMember, Object> dms = hm.get(dd);
				if (dms != null) {
					for (Iterator<DimMember> itt = dms.keySet().iterator(); itt.hasNext(); )
						sb.append(itt.next().getIdx()).append(",");
				}
				sb.append(";");
			}
		}
		return sb.toString();
	}

	public void setValueByString(String dbString) {
		HashMap<DimDef, HashMap<DimMember, Object>> hm = new HashMap<DimDef, HashMap<DimMember, Object>>();
		String[] groups = dbString.split(";");
		if (groups != null) {
			for (int i=0; i<groups.length; i++) {
				String[] dd2dms = groups[i].split("=");
				if (dd2dms != null && dd2dms.length > 0) {
					DimDef dd = DimManager.getDimDefByCode(dd2dms[0]);
					HashMap<DimMember, Object> dms = new HashMap<DimMember, Object>();
					if (dd2dms.length > 1) {
						String[] dmIdx = dd2dms[1].split(",");
						if (dmIdx != null) {
							for (int j=0; j<dmIdx.length; j++) {
								dms.put(DimManager.getDimMemberByIdx(dd, Short.parseShort(dmIdx[j])), null);
							}
						}
					}
					hm.put(dd, dms);
				}
			}
		}
		setAcceptDimVector(hm);
	}

}
